package com.itheima.Controller;

import com.itheima.Service.EmpService;
import com.itheima.entity.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/emps")
public class EmpController {
    @Autowired
    private EmpService empService;
    @GetMapping
    public Result page(EmpParam empParam){
        log.info("分页查询：{}",empParam);
        //调用Service层进行数据的封装操作
        PageBean pageBean =empService.page(empParam);
        return Result.success(pageBean);
    }
    @PostMapping
    public Result addEmp(@RequestBody Emp emp) throws Exception {
        log.info("新增员工：{}",emp);
        //1.调用Service层尽心数据的处理
        empService.addEmp(emp);
        return Result.success();
    }
    @DeleteMapping
    //通过集合类型接收传递的数组
    public Result delete(@RequestParam List<Integer> ids){
        log.info("删除员工ids={}",ids);
        empService.delete(ids);
        return Result.success();
    }
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        log.info("回显员工id={}",id);
       Emp emp= empService.getByid(id);
        return Result.success(emp);
    }
    @PutMapping
    public Result update(@RequestBody Emp emp){
        log.info("修改员工：emp={}",emp);
        //调用Service层
        empService.update(emp);
        return  Result.success();
    }
    //查询所有员工信息
    @GetMapping("/list")
    public Result getList(){
        //调用Service层
        List <Emp> empList= empService.getlist();
        return Result.success(empList);
    }
}
